#!/bin/bash

old_release_dir="./oldrelease"
tmp_dir="tmp"
dest_dir="programs"
fs_dir="rootfs"
fs_zip="rootfs.tar.gz"
tmp_zip="rf862.tar.gz"
md5_file="seed"
final_zip="rf862_upgrade.tar.gz"
cur_time=`date +%Y%m%d_%H%M%S`
prefix_bin="invengo_rf862"
version_conf="./rootfs/usr/invengo/version"

if [ -f $version_conf ]; then
	version_str=`cat $version_conf`
	final_bin="${prefix_bin}_${version_str}_${cur_time}.bin"
else
	echo "package without version info."
	final_bin="${prefix_bin}_${cur_time}.bin"
fi

clean_file(){
	rm $tmp_zip
	rm $md5_file
	rm $final_zip

	# 清理掉打包目录下面的文件，防止下一次打包又将其打包进来
	rm $tmp_dir -rf
	if [ ! -d $tmp_dir ]; then
		mkdir $tmp_dir
	fi

	cd $dest_dir
	cnt1=`ls | wc -l`
	if [ $cnt1 -ne 0 ]; then
		mv ./* "../${tmp_dir}"
	fi

	cd ../$fs_dir
	cnt2=`ls | wc -l`
	if [ $cnt2 -ne 0 ]; then
		mv ./* "../${tmp_dir}"
	fi
}

# 当前目录下有旧版本压缩包文件，拷贝至$old_release_dir目录
echo "=== 1.move old packet ..."
old_packet_cnt=`find ./ -maxdepth 1 -name "invengo_*.bin" | wc -l`
if [ $old_packet_cnt -ne 0 ]; then
	if [ ! -d $old_release_dir ]; then
		mkdir $old_release_dir
	fi
	
	#mv "${final_zip}_*" "${old_release_dir}/"
	echo "old packet exists, move to $old_release_dir"
	mv ./invengo_*.bin ./oldrelease/
fi

# 检查待打包的源目录是否存在
if [ ! -d "$dest_dir" ]; then
	echo "$dest_dir dir does not exist."
	mkdir $dest_dir
	exit 1
fi

# 检查根文件系统是否需要打包
echo "=== 2.zip rootfs ..."
# rootfs目录不存在，则认为根文件系统无更新项目
if [ ! -d "$fs_dir" ]; then
	echo "$fs_dir does not exist"
	mkdir $fs_dir
else
	cd $fs_dir
	fs_file_cnt=`ls | wc -l`
	# rootfs目录下无文件，需要返回到上层目录
	if [ $fs_file_cnt -eq 0 ]; then
		echo "rootfs no upgrade file."
		cd ../
	else
		tar zcvf $fs_zip ./*
		# rootfs目录下有文件，开始打包，打包失败直接退出脚本；否则将升级包拷贝至programs目录
		if [ $? -ne 0 ]; then
			echo "zip ${fs_zip} failed."
			exit 1
		else
			echo "move rootfs to $dest_dir"
			mv ${fs_zip} "../${dest_dir}"
			cd ../
		fi
	fi
fi

# 对源文件进行打包
echo "=== 3.zip $tmp_zip ..."
upgrade_file_cnt=`ls ${dest_dir} | wc -l`
if [ $upgrade_file_cnt -eq 0 ]; then
	echo "No upgrade file, exit"
	exit 1
fi

tar zcvf $tmp_zip $dest_dir
if [ $? -ne 0 ]; then
	echo "zip $tmp_zip failed."
	exit 1
fi

# 创建MD5校验文件
echo "=== 4.create $md5_file ..."
cp $tmp_zip "${tmp_zip}.seed"
echo "Xc8salt$" >> "${tmp_zip}.seed"
md5sum "${tmp_zip}.seed" > $md5_file

md5sum -c $md5_file
if [ $? -ne 0 ]; then
	echo "create $md5_file failed."
	exit 1
fi

# 打包最终的压缩包，含源文件压缩包和MD5文件
echo "=== 5.zip $final_zip ..."
rm "${tmp_zip}.seed"
#tar zcvf "${final_zip}_${cur_time}" $tmp_zip $md5_file
tar zcvf $final_zip $tmp_zip $md5_file
if [ $? -ne 0 ]; then
	echo "zip ${final_zip} failed."
	exit 1
fi

# 加密压缩包
echo "=== 6.zip $final_bin ..."

DIR_CUR=`pwd`
echo ${DIR_CUR}
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/mnt/extdisk1/projects/xc-rf862/upgrade_packet/tool_pc/openssl
tar -zcvf - $final_zip | LD_LIBRARY_PATH=$LD_LIBRARY_PATH:tool_pc/openssl tool_pc/openssl/openssl des3 -salt -k invengo_rf862 -out ${final_bin}
if [ $? -ne 0 ]; then
	echo "zip ${final_bin} failed."
	exit 1
fi

echo "=== 7.packaged."

# 清理打包过程中的过程文件
clean_file
